package com.thb.pay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.core.utils.StrUtil;
import com.thb.pay.db.service.impl.BaseServiceImpl;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.entity.AgentAccount;
import com.thb.pay.entity.AgentInfo;
import com.thb.pay.mapper.AgentAccountMapper;
import com.thb.pay.mapper.AgentInfoMapper;
import com.thb.pay.service.AgentInfoService;
import com.thb.pay.vo.req.AgentInfoPageReq;
import com.thb.pay.vo.req.AgentInfoReq;
import com.thb.pay.vo.resp.AgentInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.ReflectPermission;
import java.math.BigDecimal;

/**
 * <p>
 * 代理商-信息表 服务实现类
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@Service
public class AgentInfoServiceImpl extends BaseServiceImpl<AgentInfoMapper, AgentInfo> implements AgentInfoService {

    @Autowired
    private AgentAccountMapper agentAccountMapper;

    private static boolean canAccessPrivateMethods() {
        try {
            //java安全管理器具体细节，可看博客中另一篇介绍
            SecurityManager securityManager = System.getSecurityManager();
            if (null != securityManager) {
                securityManager.checkPermission(new ReflectPermission("suppressAccessChecks"));
            }
        } catch (SecurityException e) {
            return false;
        }
        return true;
    }

    @Override
    public PageResult<AgentInfoVo> findPage(AgentInfoPageReq pageReq) {
        IPage<AgentInfo> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<AgentInfo> pageResult = super.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, AgentInfoVo.class);
    }

    @Transactional
    @Override
    public void add(AgentInfoReq agentInfo) {
        AgentInfo insetAgentInfo = new AgentInfo();
        BeanUtils.copyProperties(agentInfo, insetAgentInfo);
        // 插入代理商基本信息
        super.insert(insetAgentInfo);
        // 插入代理商账户信息
        AgentAccount agentAccount = AgentAccount.builder()
                .agentId(insetAgentInfo.getId())
                .balance(BigDecimal.ZERO.longValue())
                .securityMoney(BigDecimal.ZERO.longValue())
                .settAmount(BigDecimal.ZERO.longValue())
                .unBalance(BigDecimal.ZERO.longValue())
                .todayExpend(BigDecimal.ZERO.longValue())
                .todayIncome(BigDecimal.ZERO.longValue())
                .totalIncome(BigDecimal.ZERO.longValue())
                .build();

        agentAccountMapper.insert(agentAccount);
    }

    @Override
    public AgentInfoVo getByLoginName(String loginName) {
        if (StringUtils.isBlank(loginName)) return null;
        AgentInfoVo agentInfo;
        if (StrUtil.checkEmail(loginName)) {
            agentInfo = getByEmail(loginName);
            if (agentInfo != null) return agentInfo;
        }
        if (StrUtil.checkMobileNumber(loginName)) {
            agentInfo = getByMobile(Long.parseLong(loginName));
            if (agentInfo != null) return agentInfo;
        }
        agentInfo = getByUserName(loginName);
        return agentInfo;
    }

    @Override
    public AgentInfoVo getByAgentId(Long agentId) {
        AgentInfo resResult = super.selectById(agentId);
        if (resResult != null) {
            AgentInfoVo targetResult = new AgentInfoVo();
            BeanUtils.copyProperties(resResult, targetResult);
            return targetResult;
        }
        return null;
    }

    @Override
    public AgentInfoVo getByUserName(String userName) {
        LambdaQueryWrapper<AgentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgentInfo::getUserName, userName);
        return getAgentInfoVo(queryWrapper);
    }

    @Override
    public AgentInfoVo getByMobile(Long mobile) {
        LambdaQueryWrapper<AgentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgentInfo::getMobile, mobile);
        return getAgentInfoVo(queryWrapper);
    }

    @Override
    public AgentInfoVo getByEmail(String email) {
        LambdaQueryWrapper<AgentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgentInfo::getEmail, email);
        return getAgentInfoVo(queryWrapper);
    }

    private AgentInfoVo getAgentInfoVo(LambdaQueryWrapper<AgentInfo> queryWrapper) {
        AgentInfo resResult = super.getOne(queryWrapper);
        if (resResult != null) {
            AgentInfoVo targetResult = new AgentInfoVo();
            BeanUtils.copyProperties(resResult, targetResult);
            return targetResult;
        }
        return null;
    }
}
